
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ManualPreprocess implements ActionListener
{
	IPWindow window;
	JFrame frame;
	JPanel mainPane;

	public ManualPreprocess(IPWindow _window)
	{
		window = _window;

		// Create our frame, panel and buttons.
		frame = new JFrame("Preprocess");
		mainPane = new JPanel(new BorderLayout());

		// Populate panel
		//mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.PAGE_AXIS));
		mainPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
//		mainPane.add(Box.createRigidArea(new Dimension(50,50)));
//		mainPane.add(Box.createGlue());

		// Action panel
		JPanel actionPane = new JPanel();
		actionPane.setBorder(
				BorderFactory.createCompoundBorder(
					BorderFactory.createTitledBorder("Preprocess"),
					BorderFactory.createEmptyBorder(5,5,5,5)
					));
		actionPane.setLayout(new BoxLayout(actionPane, BoxLayout.PAGE_AXIS));
		actionPane.setBackground(new Color(255,255,255));

		JButton btnMedianFilter = new JButton("Median Filter");
		JButton btnContrastInc = new JButton("Contrast Stretch Inc");		
		JButton btnContrastDec = new JButton("Contrast Stretch Dec");		
		btnMedianFilter.setActionCommand("median");
		btnContrastInc.setActionCommand("contrastInc");
		btnContrastDec.setActionCommand("contrastDec");
		btnMedianFilter.addActionListener(this);
		btnContrastInc.addActionListener(this);		
		btnContrastDec.addActionListener(this);		
		actionPane.add(btnMedianFilter);
		actionPane.add(btnContrastInc);
		actionPane.add(btnContrastDec);

		// Nav buttons
		JPanel navPane = new JPanel();
		JButton b1 = new JButton("Next");
		JButton b2 = new JButton("Close");
		b1.setActionCommand("next");
		b2.setActionCommand("close");
		b1.addActionListener(this);
		b2.addActionListener(this);
		navPane.add(b1);
		navPane.add(b2);
		navPane.setBackground(new Color(255,255,255));

		mainPane.add(actionPane, BorderLayout.CENTER);
		mainPane.add(navPane, BorderLayout.PAGE_END);
		
		// Set panel as mane pane on frame, and show frame.
		frame.setContentPane(mainPane);
		mainPane.setOpaque(true);
		mainPane.setBackground(new Color(255,255,255));
		frame.pack();
		//frame.setLocationRelativeTo(window.frame);
		frame.setVisible(true);		

		////
		// Warning messges
		if (window.getROI() == null)
		{
			p.print("Warning: ROI is null.  Verfiy image is loaded.");
		}
	}

	public void actionPerformed(ActionEvent e)
	{
		String cmd = e.getActionCommand();
		if (cmd.equals("median"))
		{
			window.applyMedianFileter();
			window.updateFrame();
		}
		else if (cmd.equals("contrastInc"))
		{
			window.applyContrastStretch(true);
			window.updateFrame();
		}
		else if (cmd.equals("contrastDec"))
		{
			window.applyContrastStretch(false);
			window.updateFrame();
		}
		else if (cmd.equals("next"))
		{
			new ManualThreshold(window);
			frame.dispose();
		}
		else if (cmd.equals("close"))
		{
			frame.dispose();
		}
	}
}
